package cn.edu.ujn.booking.controller;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import cn.edu.ujn.booking.dao.Dict;
import cn.edu.ujn.booking.service.DictService;

@Controller
public class DictController {
	@Autowired
	private DictService dictService;
	
	@RequestMapping("/addDict.action")
	@ResponseBody
	public String toAddDict(int user_id,int dramas_id,String seat,int dictid,HttpSession session) {
		System.out.println(user_id+":"+dramas_id+":"+seat+":"+dictid);
		Dict dict2 = new Dict();
		dict2.setDramas_id(dramas_id);
		dict2.setUser_id(user_id);
		List<Dict> selectSeat = dictService.selectSeat(dict2);
		System.out.println(selectSeat);
		if(selectSeat.size()>0) {
			return "SORRY";
		}else {
		
		//取当前时间
		Date date=new Date();
		//转换时间格式
		SimpleDateFormat simpleDate = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); 
		Dict dict = new Dict();
		dict.setId(dictid);
		dict.setDramas_id(dramas_id);
		dict.setSeat(seat);
		dict.setUser_id(user_id);
		dict.setAddtime(simpleDate.format(date));
		System.out.println(dict);
		int update = dictService.updateDict(dict);
		List<Dict> list = dictService.findAllDict();
		session.setAttribute("dramas_seat", list);
		if(update>0) {
			return "OK";
		}else {
			return "FAIL";
		}
		}
	}
	@RequestMapping("/updateSeat.action")
	@ResponseBody
	public String toUpdateSeat(int id,HttpSession session) {
		Dict dict = new Dict();
		dict.setId(id);
		dict.setUser_id(0);
		dict.setAddtime(null);
		System.out.println(dict);
		int dict2 = dictService.updateDict(dict);
		System.out.println(dict2);
		List<Dict> list = dictService.findAllDict();
		session.setAttribute("dramas_seat", list);
		List<Dict> detail = dictService.detail();
		session.setAttribute("detail", detail);
		if(dict2>0) {
			return "OK";
		}else {
			return "FAIL";
		}
	}

}
